package com.zy.hospitalward_management_system.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.hospitalward_management_system.pojo.Department;
import com.zy.hospitalward_management_system.pojo.Doctor;
import com.zy.hospitalward_management_system.pojo.Drug;
import com.zy.hospitalward_management_system.pojo.Inpatientward;
import com.zy.hospitalward_management_system.service.impl.DepartmentServiceImpl;
import com.zy.hospitalward_management_system.service.impl.DoctorServiceImpl;
import com.zy.hospitalward_management_system.service.impl.InpatientwardServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zy
 * @since 2021-01-26
 */
@Controller
public class DoctorController {


    @Resource
    DoctorServiceImpl doctorService;

    @Resource
    DepartmentServiceImpl departmentService;


    /**
     * 根据科室ID 查询所有所属该科室的医生
     * @param DepartmentId 科室ID
     * @return JSON格式的医生列表
     */
    @ResponseBody
    @RequestMapping("/doctorSelect")
    public String doctorSelect(int DepartmentId){
        List<Doctor> doctors = doctorService.queryDoctorsByDepartmentId(DepartmentId);
        return JSON.toJSONString(doctors);
    }


    /**
     * 添加一名医生
     * @param doctor
     * @return
     */
    @PostMapping("/doctor/add")
    @ResponseBody
    public String addDoctor(Doctor doctor){
        if(doctor.getDoctorName().isEmpty()){
            return "请输入姓名!";
        }
        if(doctor.getDoctorAge()==null){
            return "请输入年龄!";
        }
        if(doctor.getDoctorPhone().isEmpty()){
            return "请输入联系方式!";
        }

        int insert = doctorService.getBaseMapper().insert(doctor);
        if(insert==1){
            return "添加成功!";
        }else {
            return "添加失败!";
        }

    }

    /**
     * 根据ID删除一名医生
     * @param id
     * @return
     */
    @GetMapping("/doctor/delete/{id}")
    public String deleteDoctor(@PathVariable("id") int id){
        int i = doctorService.getBaseMapper().deleteById(id);
        return "redirect:/toDoctorList";
    }

    /**
     * 根据ID更新一名医生
     * @return
     */
    @PostMapping("/doctor/update")
    @ResponseBody
    public String deleteDoctor(Doctor doctor){

        if(doctor.getDoctorName().isEmpty()){
            return "请输入姓名!";
        }
        if(doctor.getDoctorAge()==null){
            return "请输入年龄!";
        }
        if(doctor.getDoctorPhone().isEmpty()){
            return "请输入联系方式!";
        }

        int i = doctorService.getBaseMapper().updateById(doctor);
        if(i==1){
            return "修改成功!";
        }else{
            return "修改失败";
        }

    }


    /**
     * 根据医生姓名或者联系方式或者ID对医生管理页面的精确或模糊查询
     */
    @RequestMapping("/doctor/search")
    public String doctorSearch(String search, Model model){
        if(search==null||search.isEmpty()){
            model.addAttribute("msg","请输入搜索内容!");
            return "forward:/toDoctorList";
        }
        search=search.trim();
        List<Map<String, String>> maps = doctorService.getBaseMapper().likeQueryAllDoctorsInfo(search);
        if(!maps.isEmpty()){
            List<Department> departments = departmentService.getBaseMapper().selectList(null);
            model.addAttribute("maps",maps);
            model.addAttribute("departments",departments);
            return "user/list/doctor_list";
        }
        model.addAttribute("msg","未查询到相关记录!");
        return "forward:/toDoctorList";
    }







}

